package com.yuuzy.view;

import com.yuuzy.dao.Buy_FundDAO;
import com.yuuzy.model.Buy_Fund;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

public class VieworderFund extends JInternalFrame {
    private JTable fund_table;
    private Buy_FundDAO buy_fundDAO = new Buy_FundDAO();
    private JTextField fund_idText;
    private JTextField client_idText;

    /**
     * Create the frame.
     */
    public VieworderFund() {
        setIconifiable(true);
        setClosable(true);
        setTitle("查看基金订单");
        setBounds(100, 100, 756, 739);

        JScrollPane scrollPane = new JScrollPane();

        JLabel lblNewLabel = new JLabel("基金编号：");

        JLabel lblNewLabel_1 = new JLabel("客户编号：");

        fund_idText = new JTextField();
        fund_idText.setColumns(10);

        client_idText = new JTextField();
        client_idText.setColumns(10);

        JButton btnNewButton = new JButton("查询 ");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                queryActionPerformed(e);
            }
        });
        btnNewButton.setIcon(new ImageIcon(VieworderFund.class.getResource("/image/pm-query-16.png")));
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(89)
                                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 554, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(160)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(lblNewLabel)
                                                        .addComponent(lblNewLabel_1))
                                                .addGap(14)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(client_idText, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(fund_idText, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE))))
                                .addContainerGap(97, Short.MAX_VALUE))
                        .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
                                .addContainerGap(446, Short.MAX_VALUE)
                                .addComponent(btnNewButton)
                                .addGap(171))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.TRAILING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(60)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel)
                                        .addComponent(fund_idText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(6)
                                                .addComponent(btnNewButton)
                                                .addGap(5)
                                                .addComponent(lblNewLabel_1))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(40)
                                                .addComponent(client_idText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 421, GroupLayout.PREFERRED_SIZE)
                                .addGap(79))
        );

        fund_table = new JTable();
        fund_table.setFillsViewportHeight(true);
        fund_table.setModel(new DefaultTableModel(
                new Object[][]{
                },
                new String[]{
                        "基金编号", "客户id", "购买时间", "购买数量", "资产状态", "收益"
                }
        ));
        fund_table.getColumnModel().getColumn(1).setPreferredWidth(94);
        fund_table.getColumnModel().getColumn(2).setPreferredWidth(132);
        fund_table.getColumnModel().getColumn(3).setPreferredWidth(168);
        fund_table.getColumnModel().getColumn(4).setPreferredWidth(193);
        fund_table.getColumnModel().getColumn(5).setPreferredWidth(173);
        scrollPane.setViewportView(fund_table);
        getContentPane().setLayout(groupLayout);

        this.fillTable(new Buy_Fund());
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    VieworderFund frame = new VieworderFund();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 查询事件处理
     *
     * @param evt
     */
    private void queryActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String fund_id = this.fund_idText.getText();
        String client_id = this.client_idText.getText();
        Buy_Fund buy_fund = new Buy_Fund(Integer.parseInt(fund_id), Integer.parseInt(client_id));
        this.fillTable(buy_fund);
    }

    /**
     * 初始化表格
     *
     * @param buy_fund
     */
    private void fillTable(Buy_Fund buy_fund) {
        DefaultTableModel dtm = (DefaultTableModel) fund_table.getModel();
        dtm.setRowCount(0);//设置为0行，清空表格
        List<Buy_Fund> orderList = buy_fundDAO.queryBuy_Fund(buy_fund);
        for (Buy_Fund rs : orderList) {
            Vector v = new Vector();
            v.add(rs.getFund_id());
            v.add(rs.getClient_id());
            v.add(rs.getBuy_fund_time());
            v.add(rs.getBuy_fund_quantity());
            v.add(rs.getBuy_fund_assetstatus());
            v.add(rs.getBuy_fund_earnings());
            dtm.addRow(v);
        }
    }
}
